home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / srcbkvt.zip / LISTBOX.ZIP / LISTDLG.PAS < prev    next >
Pascal/Delphi Source File  |  1995-11-30  |  3KB  |  145 lines

  1. {
  2.   * Unit Name: listdlg.pas
  3.   * Author:    William Stamatakis
  4.   * Created:   11 '95
  5.   *
  6.   * Note:      ListBox Editor Dialog for Items property of
  7.                Multi-Char Listbox Component (TListEditor)
  8. }
  9.  
  10. unit Listdlg;
  11.  
  12. interface
  13.  
  14. uses
  15.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  16.   Forms, Dialogs, Buttons, ExtCtrls, StdCtrls, TBillist;
  17.  
  18. type
  19.   TListEditor = class(TForm)
  20.     lstItems: TBillListBox;
  21.     btnAdd: TButton;
  22.     GroupBox1: TGroupBox;
  23.     edtItem: TEdit;
  24.     btnEdit: TButton;
  25.     btnDelete: TButton;
  26.     Bevel1: TBevel;
  27.     btnOK: TBitBtn;
  28.     btnCancel: TBitBtn;
  29.     procedure btnAddClick(Sender: TObject);
  30.     procedure btnDeleteClick(Sender: TObject);
  31.     procedure btnEditClick(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure OK_CancelClick(Sender: TObject);
  34.   private
  35.     PriorValue: String;
  36.   public
  37.     procedure SetReferenceTo(ARef: TObject);
  38.   end;
  39.  
  40. var
  41.   ListEditor: TListEditor;
  42.  
  43. const
  44.   CAP_ADD    :string = 'Add >>';
  45.   CAP_EDIT   :string = '<< Edit';
  46.   CAP_DELETE :string = 'Delete';
  47.   CAP_UPDATE :string = 'Update';
  48.   CAP_CANCEL :string = 'Cancel';
  49.  
  50. implementation
  51.  
  52. var  ListBox: TBillListBox;
  53.  
  54. {$R *.DFM}
  55.  
  56. procedure TListEditor.SetReferenceTo(ARef: TObject);
  57. begin
  58.   ListBox := (ARef as TBillListBox);
  59. end;
  60.  
  61. procedure TListEditor.btnAddClick(Sender: TObject);
  62. begin
  63.  
  64. if (edtItem.Text <> '') then
  65. begin
  66.   if (btnAdd.Caption = CAP_ADD) then        {Add button}
  67.     lstItems.Items.Add(edtItem.Text)
  68.   else                                      {Update button}
  69.   begin
  70.     lstItems.Items[lstItems.ItemIndex] := edtItem.Text;
  71.     btnAdd.Caption := CAP_ADD;
  72.     btnEdit.Caption := CAP_EDIT;
  73.     btnDelete.Enabled := True;
  74.   end;
  75. end;
  76.  
  77. end;
  78.  
  79. procedure TListEditor.btnDeleteClick(Sender: TObject);
  80. begin
  81. if (lstItems.ItemIndex > -1) then
  82.    lstItems.Items.Delete(lstItems.ItemIndex);
  83.    lstItems.SetFocus;
  84. end;
  85.  
  86. procedure TListEditor.btnEditClick(Sender: TObject);
  87. begin
  88.  
  89. if (lstItems.ItemIndex > -1) then
  90. begin
  91.    if (btnEdit.Caption = CAP_EDIT) then  {Edit button}
  92.      begin
  93.      PriorValue := edtItem.Text;
  94.      edtItem.Text := lstItems.Items[lstItems.ItemIndex];
  95.      edtItem.SetFocus;
  96.      btnAdd.Caption := CAP_UPDATE;
  97.      btnEdit.Caption := CAP_CANCEL;
  98.      btnDelete.Enabled := False;
  99.      end
  100.    else                                  {Cancel Button}
  101.      begin
  102.      edtItem.Text := PriorValue;
  103.      btnAdd.Caption := CAP_ADD;
  104.      btnEdit.Caption := CAP_EDIT;
  105.      btnDelete.Enabled := True;
  106.      end;
  107. end;
  108.  
  109. end;
  110.  
  111. procedure TListEditor.FormCreate(Sender: TObject);
  112. begin
  113.   btnAdd.Caption := CAP_ADD;
  114.   btnEdit.Caption := CAP_EDIT;
  115.   btnDelete.Caption := CAP_DELETE;
  116. end;
  117.  
  118. procedure TListEditor.OK_CancelClick(Sender: TObject);
  119. begin
  120.   if (Sender = btnOK) then
  121.   begin
  122.  
  123.       if (btnAdd.Caption <> CAP_ADD) then
  124.       begin
  125.         ShowMessage('Select Update or Cancel before pressing OK.');
  126.         ModalResult := mrNone;
  127.       end
  128.       else
  129.         begin
  130.         ListBox.Clear;
  131.         ListBox.Items.AddStrings(lstItems.Items);
  132.         ModalResult := mrOk;
  133.         end;
  134.   end
  135.   else if (Sender = btnCancel) then
  136.   begin
  137.       if (btnEdit.Caption = CAP_CANCEL) then
  138.         btnEditClick(btnEdit);
  139.  
  140.       ModalResult := mrCancel;
  141.   end;
  142. end;
  143.  
  144. end.
  145.